package org.jetlinks.pro.things.configuration;

import org.jetlinks.core.defaults.DeviceThingsRegistrySupport;
import org.jetlinks.core.device.DeviceRegistry;
import org.jetlinks.core.event.EventBus;
import org.jetlinks.core.things.DefaultThingsDataManager;
import org.jetlinks.core.things.ThingsDataManager;
import org.jetlinks.core.things.ThingsDataManagerSupport;
import org.jetlinks.core.things.ThingsRegistry;
import org.jetlinks.pro.things.ThingsDataRepository;
import org.jetlinks.pro.things.ThingsDataRepositorySupport;
import org.jetlinks.pro.things.data.NoneStorageStrategy;
import org.jetlinks.pro.things.impl.DefaultThingsConnectorManager;
import org.jetlinks.pro.things.impl.DefaultThingsDataManagerSupport;
import org.jetlinks.pro.things.impl.DefaultThingsDataRepository;
import org.jetlinks.pro.things.impl.DefaultThingsManagerContext;
import org.jetlinks.pro.things.impl.connector.script.ThingMessageScript;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class ThingsConfiguration {

    @Bean
    public NoneStorageStrategy noneStorageStrategy() {
        return new NoneStorageStrategy();
    }

    @Bean
    public ThingsDataRepository thingsDataRepository(ObjectProvider<ThingsDataRepositorySupport> supports) {
        return new DefaultThingsDataRepository(supports);
    }

    @Bean
    public ThingsDataManager thingsDataManager(ThingsRegistry registry,
                                               ObjectProvider<ThingsDataManagerSupport> supports) {
        DefaultThingsDataManager manager = new DefaultThingsDataManager(registry);
        supports.forEach(manager::addSupport);
        ThingMessageScript.RECENT_DATA_MANAGER = manager;
        return manager;
    }

    @Bean
    @Primary
    public ThingsRegistry thingsRegistry() {
        return new AutoRegisterThingsRegistry();
    }


    @Bean
    @ConditionalOnBean(DeviceRegistry.class)
    public DeviceThingsRegistrySupport deviceThingsRegistrySupport(DeviceRegistry registry) {
        return new DeviceThingsRegistrySupport(registry);
    }

    @Bean
    public DefaultThingsManagerContext thingsManagerContext() {
        return new DefaultThingsManagerContext();
    }

    @Bean
    @SuppressWarnings("all")
    public DefaultThingsConnectorManager thingsConnectorManager(EventBus eventBus,
                                                                ThingsRegistry registry) {
        return new DefaultThingsConnectorManager(eventBus, registry);
    }

    @Bean
    @SuppressWarnings("all")
    public DefaultThingsDataManagerSupport defaultThingsDataManagerSupport(ThingsDataRepository manager,
                                                                           EventBus eventBus) {
        return new DefaultThingsDataManagerSupport(manager, eventBus);
    }

}
